.TH std::swap(std::tuple) 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::swap(std::tuple) \- std::swap(std::tuple)

.SH Synopsis
   Defined in header <tuple>
   template< class... Types >

   void swap( std::tuple<Types...>& lhs,                                  \fI(since C++11)\fP
                                                                          \fI(until C++20)\fP
              std::tuple<Types...>& rhs ) noexcept(/* see below
   */);
   template< class... Types >

   constexpr void swap( std::tuple<Types...>& lhs,                \fB(1)\fP     \fI(since C++20)\fP

                        std::tuple<Types...>& rhs ) noexcept(/*
   see below */);
   template< class... Types >

   constexpr void swap( const std::tuple<Types...>& lhs,              \fB(2)\fP (since C++23)

                        const std::tuple<Types...>& rhs )
   noexcept(/* see below */);

   Swaps the contents of lhs and rhs. Equivalent to lhs.swap(rhs).

   1) This overload participates in overload resolution only if
   std::is_swappable_v<Ti> is true for all i from 0 to sizeof...(Types).
   2) This overload participates in overload resolution only if           \fI(since C++17)\fP
   std::is_swappable_v<const Ti> is true for all i from 0 to
   sizeof...(Types).

.SH Parameters

   lhs, rhs - tuples whose contents to swap

.SH Return value

   \fI(none)\fP

.SH Exceptions

   noexcept specification:
   noexcept(noexcept(lhs.swap(rhs)))

.SH Example


// Run this code

 #include <iostream>
 #include <string>
 #include <tuple>

 int main()
 {
     std::tuple<int, std::string, float> p1{42, "ABCD", 2.71}, p2;
     p2 = std::make_tuple(10, "1234", 3.14);

     auto print_p1_p2 = [&](auto rem)
     {
         std::cout << rem
                   << "p1 = {" << std::get<0>(p1)
                   << ", "     << std::get<1>(p1)
                   << ", "     << std::get<2>(p1) << "}, "
                   << "p2 = {" << std::get<0>(p2)
                   << ", "     << std::get<1>(p2)
                   << ", "     << std::get<2>(p2) << "}\\n";
     };

     print_p1_p2("Before p1.swap(p2): ");
     p1.swap(p2);
     print_p1_p2("After  p1.swap(p2): ");
     swap(p1, p2);
     print_p1_p2("After swap(p1, p2): ");
 }

.SH Output:

 Before p1.swap(p2): p1 = {42, ABCD, 2.71}, p2 = {10, 1234, 3.14}
 After  p1.swap(p2): p1 = {10, 1234, 3.14}, p2 = {42, ABCD, 2.71}
 After swap(p1, p2): p1 = {42, ABCD, 2.71}, p2 = {10, 1234, 3.14}

.SH See also

   swap                 swaps the contents of two tuples
                        \fI(public member function)\fP
   std::swap(std::pair) specializes the std::swap algorithm
   \fI(C++11)\fP              \fI(function template)\fP

.SH Category:
     * conditionally noexcept
